package com.community.web.master.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.model.entity.Visitor;
import com.community.web.master.domain.dto.PageVisitorDto;
import com.community.web.master.domain.vo.GoodsWithCategoryNameVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.domain.vo.VisitorWithNameVo;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.VisitorMapper;
import com.community.web.master.service.IVisitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper, Visitor> implements IVisitorService {
    @Autowired
    VisitorMapper visitorMapper;

    /**
     * 访客分页查询
     * @param pageVisitorDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageVisitorDto pageVisitorDto) {
        QueryWrapper<Visitor> totalWrapper = new QueryWrapper<Visitor>();
        if(pageVisitorDto.getVisitorStatus()!=null){
            totalWrapper.eq("visitor_status",pageVisitorDto.getVisitorStatus());
        }
        Long total = visitorMapper.selectCount(totalWrapper);
        Map<String,Object> map=new HashMap<>();
        map.put("limit",(pageVisitorDto.getPageSize()));
        map.put("offset",(pageVisitorDto.getPage()-1)*pageVisitorDto.getPageSize());
        map.put("visitor_status",pageVisitorDto.getVisitorStatus());
        List<VisitorWithNameVo> result= visitorMapper.selectPageByMap(map);
        return new PageResult(total,result);
    }

    /**
     * 更新访客状态
     */
    @Override
    public void updateStatus(Integer id) {
        Visitor visitor = visitorMapper.selectById(id);
        if(visitor==null) throw new DataException("访客不存在");
        visitor.setVisitorStatus(1);
        visitorMapper.updateById(visitor);
    }
}
